package com.east.sng.dao;

import com.east.sng.entity.param.MenuParam;
import com.east.sng.entity.result.MenuFind;
import com.east.sng.entity.result.MenuInfo;
import com.east.sng.entity.result.MenuTree;
import com.east.sng.utils.RedisCache;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author east
 * @date: 2021/1/5
 * @Description:
 **/
@Repository
@CacheNamespace(implementation = RedisCache.class)
public interface MenuDao {

	/**
	 * 查询全部菜单信息
	 * @param params 参数列表
	 * @return 菜单实例对象列表
	 */
	List<MenuInfo> selectMenus(Map<String, Object> params);

	/**
	 * 查询全部一级菜单信息
	 * @return 一级菜单选项列表
	 */
	@Select("select menu_id menuId,menu_name menuName from menu where flag = 0 and parent_id = 0 and mlevel = 1")
	List<Map<String,Object>> selectParentMenus();

	/**
	 * 根据id查询菜单信息
	 * @param menuId id
	 * @return 菜单实例对象
	 */
	@Select("select menu_id, menu_name, url, code, parent_id, mlevel from menu " +
			"where flag = 0 and menu_id = #{menuId} ")
	MenuFind selectMenu(Integer menuId);

	/**
	 * 添加菜单信息
	 * @param menu 菜单实例对象
	 */
	@Insert("insert into menu(menu_name, url, code, parent_id, mlevel) " +
			"values (#{menu.menuName}, #{menu.url}, #{menu.code}, #{menu.parentName}, #{menu.mlevel}) ")
	void insertMenu(@Param("menu") MenuParam menu);

	/**
	 * 修改菜单信息
	 * @param menu 菜单实例对象
	 */
	@Update("update menu set " +
			"menu_name = #{menu.menuName}," +
			"url = #{menu.url}," +
			"parent_id = #{menu.parentName}," +
			"code = #{menu.code}," +
			"mlevel = #{menu.mlevel}" +
			"where menu_id = #{menu.menuId} ")
	void updateMenu(@Param("menu") MenuParam menu);

	/**
	 * 批量删除或恢复菜单信息
	 * @param menuIds 菜单id列表
	 * @param flag 删除标志
	 * @return 受影响行数
	 */
	@Delete({"<script>","update menu set flag = #{flag} where menu_id in",
			"<foreach collection='list' item='menuId' open='(' separator=',' close=')'>",
			"#{menuId}",
			"</foreach>",
			"</script>"})
	Integer batchDeleteOrRecoverMenus(@Param("list") List<Integer> menuIds,@Param("flag") Integer flag);

	/**
	 * 根据父id查询全部对应的二级菜单
	 * @param pid 父id
	 * @return 二级菜单列表
	 */
	@Select("select menu_id,menu_name name,url,parent_id from menu where parent_id = #{pid} and flag = 0")
	List<MenuTree> selectMenuByPid(Integer pid);

	/**
	 * 根据角色id查询全部授予的菜单id
	 * @param roleId 角色id
	 * @return 对应角色授予的菜单
	 */
	@Select("select menu_id from role2menu where role_id = #{roleId}")
	List<Integer> selectAssignMenuIdsByRoleId(Integer roleId);

	/**
	 * 根据角色id查询全部授予的菜单
	 * @param roleId 角色id
	 * @return 对应角色授予的菜单
	 */
	@Select("select menu_id,menu_name name,url,parent_id from menu where menu_id in (select menu_id from role2menu where role_id = #{userId}) and flag = 0")
    List<MenuTree> selectMenuByRoleId(Integer roleId);

}
